package me.buick.util.jmeter.snmp.snmpmgmt;

import static me.buick.util.jmeter.snmp.SNMPSamplerConstants.SERVER_TYPE_LINUX;

import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import me.buick.util.snmp.core.SupportedOS;

/**
 *
 * Version 1.0
 * 
 * This class is used to do some pre-process between SNMPSampler & SNMPDataAdaptor
 * 
 * @author buick
 *
 */

public class SNMPSampleResultProcessor {
	
	static final Logger log 	        = LoggingManager.getLoggerForClass();
	
	private String server 		 	    = "127.0.0.1";
	private int port   		     	    = 161;
	private String snmpSuiteID   	    = "";
	private String serverType           = SERVER_TYPE_LINUX;
	private int snmpDataRetriveInterval = 1500; 
	
	private SNMPDataAdaptor     adaptor;
	
	public SNMPSampleResultProcessor(String server, String port, String snmpSuiteID, String serverType, String snmpDataRetriveInterval){
		
		this.server        = server;
		this.serverType    = serverType;
		this.snmpSuiteID   = snmpSuiteID;
		try{
			this.port = Integer.parseInt(port);
			this.snmpDataRetriveInterval = Integer.parseInt(snmpDataRetriveInterval);
		}catch(Exception e){
			log.error(e.getLocalizedMessage(), e);
		}

	}
	
	public SNMPSampleResult readSampleResult(SNMPSampleResult rs) throws Exception{
		
		SupportedOS os = SupportedOS.Linux;
		
		rs.setServer(this.server);
		rs.setPort(this.port+"");
		rs.setServerType(this.serverType);
		rs.setSnmpSuiteID(this.snmpSuiteID);
		
		if(this.serverType != null && !this.serverType.toLowerCase().contains("linux")){
			os = SupportedOS.Windows;
		}
		
		rs.setSnmpDataRetriveInterval(this.snmpDataRetriveInterval+"");
		if(adaptor == null)
			adaptor = new SNMPDataAdaptor(os, this.server, this.port, this.snmpDataRetriveInterval);
		adaptor.fillSampleResult(rs);
		return rs;
	}
	
	public SNMPDataAdaptor getAdaptor(){
		return this.adaptor;
	}
}
